package com.chance.cc.crawler.development.command.script;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.script.clazz.ClazzScriptLoader;
import com.chance.cc.crawler.core.script.consul.ConsulScriptContent;
import com.chance.cc.crawler.core.script.crawler.CrawlerGroovyInstance;
import com.chance.cc.crawler.core.script.crawler.CrawlerScriptManagement;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * created by CC on 2020/11/24
 * mail 279020185@qq.com
 */
@Slf4j
public class ScriptCommand extends com.chance.cc.crawler.development.command.HttpCommand {

    private String scriptUrlPrefix ;

    private CrawlerScriptManagement scriptManagement;

    private String scriptProjectDir;

    private String scriptProject;

    public ScriptCommand(String host,int port){
        this(host,port,null,null,null);
    }

    public ScriptCommand(String host,int port,String clazzLoaderScope){
        this(host,port,null,null,clazzLoaderScope);
    }

    public ScriptCommand(String host,int port,String scriptProjectDir,String scriptProject,String clazzLoaderScope){
        super(host,port);
        this.scriptProjectDir = scriptProjectDir;
        this.scriptProject = scriptProject;
        initScriptManagement(clazzLoaderScope);
    }

    public HttpPage publishCrawlerScript2Consul(String domain){
        String url = commandUrl("addOrUpdate");
        List<ConsulScriptContent> scriptContents = scriptManagement.loadConsulContents(domain, CrawlerEnum.CrawlerScriptType.crawler);
        if (scriptContents == null || scriptContents.size()<=0){
            log.error("cant find scripts by domain {}",domain);
            return null;
        }
        HttpRequest request = new HttpRequest();
        request.setUrl(url);
        request.setMethod(HttpConstant.Method.POST);
        request.setRequestBody(HttpRequestBody.json(JSON.toJSONString(scriptContents),"utf-8"));
        HttpPage httpPage = doCommand(request);
        log.info("publish result :");
        log.info(httpPage.getRawText());
        return httpPage;
    }

    public HttpPage publishScheduleScript2Consul(String domain){
        String url = commandUrl("addOrUpdate");
        List<ConsulScriptContent> scriptContents = scriptManagement.loadConsulContents(domain,CrawlerEnum.CrawlerScriptType.scheduler);
        if (scriptContents == null || scriptContents.size()<=0){
            log.error("cant find scripts by domain {}",domain);
            return null;
        }
        HttpRequest request = new HttpRequest();
        request.setUrl(url);
        request.setMethod(HttpConstant.Method.POST);
        request.setRequestBody(HttpRequestBody.json(JSON.toJSONString(scriptContents),"utf-8"));
        HttpPage httpPage = doCommand(request);
        log.info("publish result :");
        log.info(httpPage.getRawText());
        return httpPage;
    }

    public String initUrlPrefix(String host,int port) {
        return StringUtils.join("http://",host,":",port,"/crawler/script/api/v1");
    }

    private void initScriptManagement(String clazzLoaderScope){
        //init script management

        this.scriptManagement = new CrawlerScriptManagement();
        ClazzScriptLoader loader = null;
        if (StringUtils.isNotBlank(clazzLoaderScope)){
            loader = new ClazzScriptLoader(new CrawlerGroovyInstance(),null,null,clazzLoaderScope);
        }else{
            loader = new ClazzScriptLoader(new CrawlerGroovyInstance());
        }
        this.scriptManagement.registerLoader(loader);
    }

}
